//
//  GetComplainVm.swift
//  Pods
//
//  Created by eme on 2017/4/7.
//
//

import UIKit
import RxSwift
import NetWorkCore
import ModelProtocol

class GetComplainVm{
    
    let getComplainType = PublishSubject<String>()
    
    let backDate = PublishSubject<ComplainTypeArrModel>()
    
    let disposeBag = DisposeBag()
    
    let error = PublishSubject<MikerError>()
    
    let refreshing = Variable(false)
    
    //-----
    let submitComplain = PublishSubject<String>()
    
    let submitbackDate = PublishSubject<Bool>()
    
    init (){
        
        let request = getComplainType.map{ orderid in
            
            return MineApi.getComplainType()
            
            }.shareReplay(1)
        
        let response = request.emeRequestApiForObj(ComplainTypeArrModel.self).shareReplay(1)
        
        response.subscribe(onNext:{[unowned self] result in
            
            switch result{
                
            case .success(let data):
                
                self.backDate.onNext(data)
                
            case.failure(let error):
                
                self.error.onNext(error)
                
            }
            
        }).addDisposableTo(disposeBag)
        
        
        
        let submitRequest = submitComplain.map{ jsonStr in
            return MineApi.submitComplain(jsonStr: jsonStr)
            }.shareReplay(1)
        
        let submitResponse = submitRequest.emeRequestApiForRegJson().shareReplay(1)
        
        submitResponse.subscribe(onNext:{[unowned self] result in
            switch result{
            case .success(_):
                self.submitbackDate.onNext(true)
            case .failure(let error):
                self.error.onNext(error)
            }
        }).addDisposableTo(disposeBag)
    }
    
}
